package com.leecode.xiehf.page_03;

import com.leecode.xiehf.page_19.Solution_0897;

import java.util.*;

public class Solution_0102 {
    public static void main(String[] args) {
        Solution_0102 solution = new Solution_0102();
        solution.levelOrder(new TreeNode(1));
    }

    public List<List<Integer>> levelOrder(TreeNode root) {
        Map<Integer, List<Integer>> map = new TreeMap<>();
        bst(root, 0, map);
        List<List<Integer>> list = new ArrayList<>();
        map.forEach((k, value) -> {
            list.add(value);
        });
        return list;
    }

    public void bst(TreeNode node, Integer deep, Map<Integer, List<Integer>> map) {
        if (node == null) {
            return;
        }
        bst(node.left, ++deep, map);
        if (map.containsKey(deep)) {
            map.get(deep).add(node.val);
        } else {
            List<Integer> list = new ArrayList<>();
            list.add(node.val);
            map.put(deep, list);
        }
        bst(node.right, ++deep, map);
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
